package org.yzh.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.yzh.commons.util.PageResultDTO;
import org.yzh.commons.util.StrUtils;
import org.yzh.web.entity.ElectronicFence;
import org.yzh.web.mapper.ElectronicFenceMapper;
import org.yzh.web.request.ElectronicFenceAddRequest;
import org.yzh.web.request.ElectronicFencePageSearchRequest;
import org.yzh.web.response.ElectronicFencePageSearchResponse;
import org.yzh.web.service.ElectronicFenceService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 电子围栏表 服务实现类
 * </p>
 *
 * @author hy
 * @since 2023-10-31
 */
@Service
public class ElectronicFenceServiceImpl extends ServiceImpl<ElectronicFenceMapper, ElectronicFence> implements ElectronicFenceService {
    @Resource
    private ElectronicFenceMapper electronicFenceMapper;


    @Override
    public void save(ElectronicFenceAddRequest electronicFenceAddRequest) {
        ElectronicFence electronicFence = new ElectronicFence();
        BeanUtils.copyProperties(electronicFenceAddRequest,electronicFence);
        electronicFence.setGmtCreate(LocalDateTime.now());
        electronicFence.setGmtModified(LocalDateTime.now());
        electronicFence.setIsValid(1);
        electronicFenceMapper.save(electronicFence);
    }

    @Override
    public void update(ElectronicFenceAddRequest electronicFenceAddRequest) {
        ElectronicFence electronicFence = new ElectronicFence();
        BeanUtils.copyProperties(electronicFenceAddRequest,electronicFence);
        electronicFence.setGmtModified(LocalDateTime.now());
        electronicFenceMapper.upDate(electronicFence);
    }

    @Override
    public void remove(List<Integer> ids) {
        electronicFenceMapper.remove(ids);
    }


    @Override
    public PageResultDTO<ElectronicFencePageSearchResponse> fencePage(ElectronicFencePageSearchRequest request) {

        LambdaQueryWrapper<ElectronicFence> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ElectronicFence::getIsValid, 1);
        if (request.getId() != null) {
            queryWrapper.like(ElectronicFence::getId, request.getId());
        }
        if (StrUtils.isNotBlank(request.getFenceName())) {
            queryWrapper.like(ElectronicFence::getFenceName, request.getFenceName());
        }
        queryWrapper.orderByAsc(ElectronicFence::getSerialNumber);
//        Page<ElectronicFence> of = Page.of(request.getPageNum(), request.getPageSize());
//        Page<ElectronicFence> page = this.page(of);
        Page<ElectronicFence> selectPage = this.baseMapper.selectPage(new Page<>(request.getPageNum(), request.getPageSize()), queryWrapper);
        if (selectPage.getRecords()
                .isEmpty()) {
            return PageResultDTO.empty();
        }

            return PageResultDTO.convert(selectPage, ElectronicFencePageSearchResponse::convert);
        }

    }

